package service.article.managers;

import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.Registry;
import java.util.ArrayList;

import dbExceptions.BadComboException;
import dbExceptions.DataBaseErrorException;
import dbExceptions.MissingComboException;
import article.entities.ArticleVO;
import article.entities.CategoryVO;
import article.entities.ComboVO;
import article.entities.IngredientVO;
import article.exceptions.BadArticleException;
import article.exceptions.BadCategoryException;
import article.exceptions.IncompleteArticleException;
import article.exceptions.MissingArticleException;
import article.exceptions.MissingCategoryException;
import article.exceptions.NotHereException;
import article.interfaces.ArticleRemoteMgt;
import service.Factory;
import service.article.exceptions.ProxyException;
import service.article.interfaces.ArticleProxyMgt;

public class ArticleProxyMgr implements ArticleProxyMgt {

	private static ArticleProxyMgr instance = null;

	private ArticleProxyMgr() {
	}

	public static ArticleProxyMgr getInstance() {
		if (instance == null)
			instance = new ArticleProxyMgr();
		return instance;
	}

	public void addArticle(ArticleVO artNew) throws IncompleteArticleException,
			ProxyException, RemoteException, MissingCategoryException,
			DataBaseErrorException, BadCategoryException,
			MissingArticleException {
		ArticleRemoteMgt aux = this.getArticleRemoteMgt();
		aux.addArticle(artNew);

	}

	public void updateArticle(ArticleVO artUpt) throws NotHereException,
			IncompleteArticleException, ProxyException, RemoteException,
			MissingCategoryException, DataBaseErrorException,
			BadCategoryException, MissingArticleException, BadArticleException {
		ArticleRemoteMgt aux = this.getArticleRemoteMgt();
		aux.updateArticle(artUpt);
	}

	public void deleteArticle(ArticleVO artDel) throws NotHereException,
			ProxyException, RemoteException, MissingArticleException,
			BadArticleException, DataBaseErrorException {
		ArticleRemoteMgt aux = this.getArticleRemoteMgt();
		aux.deleteArticle(artDel);
	}

	
	public ArticleVO[] getArticles() throws ProxyException, RemoteException {
		ArticleRemoteMgt aux = this.getArticleRemoteMgt();
		return aux.getArticles();
	}

	public ArticleVO[] getAllArticles() throws ProxyException,
			RemoteException {
		ArrayList<ArticleVO> aux = this.getArticleRemoteMgt().getAllArticles();
		return (aux!= null) ? aux.toArray(new ArticleVO[aux.size()]) : null;
	}

	public IngredientVO[] stringToIngr(String str) {
		String[] temp = str.split(" ");
		IngredientVO[] aux = new IngredientVO[temp.length];
		int i = 0;
		for (String e : temp) {
			aux[i] = new IngredientVO(e);
			i++;
		}
		return aux;
	}

	
	public IngredientVO[] getAllIngredients() throws ProxyException,
			RemoteException {
		ArticleRemoteMgt aux = this.getArticleRemoteMgt();
		return aux.getAllIngredients();
	}

	
	public IngredientVO[] getInStockIngredients() throws ProxyException,
			RemoteException {
		ArticleRemoteMgt aux = this.getArticleRemoteMgt();
		return aux.getInStockIngredients();
	}

	
	public IngredientVO[] getOutStockIngredients() throws ProxyException,
			RemoteException {
		ArticleRemoteMgt aux = this.getArticleRemoteMgt();
		return aux.getOutStockIngredients();
	}
	
	
	public void addOutOfStock(ArrayList<IngredientVO> ingredientes)
			throws RemoteException, ProxyException {
		this.getArticleRemoteMgt().addOutOfStock(ingredientes);
	}

	
	public void addInStock(ArrayList<IngredientVO> ingredientes)
			throws RemoteException, ProxyException {
		this.getArticleRemoteMgt().addInStock(ingredientes);		
	}

	
	public void newIngredient(String name) throws ProxyException,
			RemoteException {
		ArticleRemoteMgt aux = this.getArticleRemoteMgt();
		aux.newIngredient(name);
	}

	
	public CategoryVO[] getCategories() throws ProxyException, RemoteException {
		ArticleRemoteMgt aux = this.getArticleRemoteMgt();
		return aux.getCategories();
	}

	
	public void newCategory(String name) throws ProxyException, RemoteException {
		ArticleRemoteMgt aux = this.getArticleRemoteMgt();
		aux.newCategory(name);
	}	

	
	public void newCombo(ComboVO comb) throws MissingCategoryException,
			MissingComboException, BadCategoryException, RemoteException,
			ProxyException {
		this.getArticleRemoteMgt().newCombo(comb);		
	}

	
	public void deleteCombo(ComboVO comb) throws MissingArticleException,
			BadArticleException, DataBaseErrorException, RemoteException,
			ProxyException {
		this.getArticleRemoteMgt().deleteCombo(comb);		
	}

	public void updateCombo(ComboVO comb) throws MissingComboException,
			BadComboException, BadCategoryException, MissingCategoryException,
			DataBaseErrorException, RemoteException, ProxyException {
		this.getArticleRemoteMgt().updateCombo(comb);		
	}
	
	public ComboVO[] getCombos() throws RemoteException, ProxyException {
		return this.getArticleRemoteMgt().getCombos();
	}

	private ArticleRemoteMgt getArticleRemoteMgt() throws ProxyException {
		ArticleRemoteMgt artRem = null;
		try {
			String sCom = Factory.getInstance().getArticle();
			Registry oRegitry = Factory.getInstance().getRegistry();
			artRem = (ArticleRemoteMgt) oRegitry.lookup(sCom);
		} catch (RemoteException e) {
			e.printStackTrace();
		} catch (NotBoundException e) {
			e.printStackTrace();
		}
		if (artRem == null)
			throw new ProxyException("Error en el RMI");
		return artRem;
	}


}
